package com.woniuxy.course.controller;

import com.woniuxy.commons.dto.CourseCategoryDto;
import com.woniuxy.commons.entity.CourseCategory;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.utils.CodeUtils;
import com.woniuxy.commons.utils.RegexUtils;
import com.woniuxy.course.service.CourseCategoryService;
import com.woniuxy.course.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author LuoChen
 * @DateTime 2020-07-16 18:40:15
 * @Description CourseCategory控制层
 */
@RestController
@RequestMapping("/courseCategory")
public class CourseCategoryController {

    @Autowired
    private CourseCategoryService courseCategoryService;

    /**
     * 处理前端发来的获取所有课程类别的请求，并将数据结果和信息响应回去
     *
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.util.List < com.woniuxy.commons.entity.CourseCategory>>
     * @author: LuoChen
     * @dateTime: 2020-07-16 20:22:59
     */
    @GetMapping("/all")
    public ResponseEntity<List<CourseCategory>> findAllCategories() {
        ResponseEntity<List<CourseCategory>> listResponseEntity = new ResponseEntity<>();
        List<CourseCategory> categories = courseCategoryService.findAll();
        if (categories != null) {
            return listResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setData(categories)
                    .setMessage("这是所有的课程类别");
        }
        return listResponseEntity.setMessage("没有任何数据，是不是出了什么问题？").setCode(StatusCode.RESPONSE_NULL);
    }

    @GetMapping("/nodeTree")
    public ResponseEntity<Map<Integer, CourseCategoryDto>> findCategoriesTree() {
        ResponseEntity<Map<Integer, CourseCategoryDto>> listResponseEntity = new ResponseEntity<>();
        Map<Integer, CourseCategoryDto> categoryBeTree = courseCategoryService.getCategoryBeTree();
        System.out.println("=============  数据  ==================");
        System.out.println(categoryBeTree);
        if (categoryBeTree != null) {
            return listResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setData(categoryBeTree)
                    .setMessage("这是所有的课程类别");
        }
        return listResponseEntity.setMessage("没有任何数据，是不是出了什么问题？").setCode(StatusCode.RESPONSE_NULL);
    }


    /**
     * 处理前端的新增课程分类的请求，并将数据结果和信息响应回去
     *
     * @param categoryName
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.lang.Void>
     * @author: LuoChen
     * @dateTime: 2020-07-16 20:23:14
     */
    @PostMapping("/add")
    public ResponseEntity<Void> addNewCategory(@RequestParam("categoryName") String categoryName) {
        ResponseEntity<Void> responseEntity = new ResponseEntity<>();
        if (!RegexUtils.nameIsRight(categoryName)) {
            return responseEntity.setCode(StatusCode.ILLEGAL_STRING).setMessage("输入的课程类别名不合法");
        }
        if (courseCategoryService.addCategory(categoryName) == StatusCode.OPERATION_SUCCESS) {
            return ResponseEntity.SUCCESS_RESPONSE.setMessage("新增课程类别成功");
        }
        return responseEntity.setMessage("系统异常").setCode(StatusCode.SERVER_ERROR);
    }

    /**
     * 处理前端删除类别的请求
     * @param categoryId
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.lang.Void>
     * @author: LuoChen
     * @dateTime: 2020-07-16 21:03:45
     */
    @DeleteMapping("/delete/{categoryId}")
    public ResponseEntity<Void> deleteCategory(@PathVariable("categoryId") int categoryId){
        ResponseEntity<Void> responseEntity = new ResponseEntity<>();
        if (courseCategoryService.deleteCategory(categoryId) == StatusCode.OPERATION_SUCCESS) {
            return ResponseEntity.SUCCESS_RESPONSE.setMessage("删除课程类别成功");
        }
        return responseEntity.setMessage("系统异常").setCode(StatusCode.SERVER_ERROR);
    }
}
